<?php


namespace app\api\controller;

//http://cosmo.com/api/goods/getGood
class Goods extends BaseApi {

    public function getGoodsList(){
        //商品分类:1=天天特价,2=一元抢购
        if( !$this->request->param('pdata')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $page = max(1,intval($this->request->param('page')));
        $pdata = intval($this->request->param('pdata'));
        $pagesize = intval($this->request->param('pagesize'));

        $goods = new \app\api\model\Goods();
        $goodsList = $goods->getGoodsList(['pdata'=>$pdata],$page,$pagesize);
        if(!$goodsList->isEmpty()){
            foreach ($goodsList as $list){
                $list =  $this->dealGood($list);
            }
        }
        return json(['code'=>1,'data'=>$goodsList]);
    }

    public function getGood(){
        if( !$this->request->param('goodsid')){
            return json(['code'=>-1,'msg'=>'缺少参数']);
        }

        $goodsid = intval($this->request->param('goodsid'));
        $goods = new \app\api\model\Goods();
        $goodsItem = $goods->getGoodsDetail($goodsid);

        if(!empty($goodsItem)){
            $goodsItem = $this->dealGood($goodsItem);
            return json(['code'=>1,'data'=>$goodsItem]);
        }

        return json(['code'=>-1,'msg'=>'暂无数据']);
    }

    protected function dealGood($goods){
        if($goods->image){
            $goods->image = $this->imageUrl.$goods->image;
        }
        if($goods->images){
            $imagesArray = [];
            $tmpImage = explode(',',$goods->images);
            foreach ($tmpImage as $tmp){
                $imagesArray[] = $this->imageUrl.$tmp;
            }
            $goods->images = $imagesArray;
        }
        if($goods->describe){
            $goods->describe = $this->replacePath($goods->describe);
        }
        return $goods;
    }
}


